package com.doyens.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.doyens.mapper.ScheduleMapper;
import com.doyens.mapper.StudentMapper;
import com.doyens.pojo.DTO;
import com.doyens.pojo.Student;
import com.doyens.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper mapper;
    @Autowired
    private ScheduleMapper scheduleMapper;


    public PageInfo<Student> findAll(String pageNum, Integer pageSize) {
        try {
            if (StringUtils.isEmpty(pageNum)) {
                pageNum = "1";
                pageSize = 2;
                PageHelper.startPage(Integer.parseInt(pageNum), pageSize);
            } else {
                PageHelper.startPage(Integer.parseInt(pageNum), pageSize);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return new PageInfo<Student>(mapper.findAll());
    }

    /**
     * 根据id删除
     * 注意删除顺序
     */
    public int del(Integer id) {
        //先删除课程
        mapper.delById(id);
        //再删除学生对象
        return mapper.del(id);
    }

    public void save(Student student, Integer[] scheduleIds) {
        mapper.save(student);
        //根据id存储学生对应的学习的课程
        for (Integer scheduleId : scheduleIds) {
            mapper.saveSd(student.getSid(), scheduleId);
        }
    }

    /**
     * 以此判断携带的参数是否有值
     * 根据不同的参数查询不同的条件
     */
    public void findByCondition(DTO dto) {
        mapper.findByCondition(dto);
    }
}
